<?php

namespace App\Form;

use App\Dao\Ad\AdDao;
use App\Dao\Exam\ExamDao;
use Yj\Apidog\Annotation\ApiController;
use Yj\Apidog\Annotation\FormData;
use Yj\Apidog\Annotation\PostApi;
use Yj\ColumnBuilder\Column;
use Yj\Form\Elm;

/**
 * @ApiController(prefix="form/exam")
 */
class Exam extends BaseForm
{
    protected $yjCode = 'exam';

    protected $routes = [
        'lists'=>'/admin/exam/exam/lists',
        'add'=>'admin/exam/exam/add',
        'edit'=>'admin/exam/exam/edit',
        'form'=>'/form/exam/update',
    ];

    public function getColumn(){
        $colum = Column::create();
        $colum->setRules([
            Column::txt()->setLabel('标题')->setProp('title'),
            Column::txt()->setLabel('创建时间')->setProp('create_time'),
        ]);
        return $colum->toArray();
    }

    protected function getRoute($types=null){
        if(is_null($types)){
            return $this->routes;
        }
        return $this->routes[$types];
    }

    /**
     * @PostApi(path="config",description="配置")
     */
    public function config(){
       $routes =  $this->getRoute();
       $column = $this->getColumn();
       _SUCCESS(compact('routes','column'));
    }

    /**
     * @PostApi(path="update",description="添加")
     * @FormData(key="id",rule="required",default="0")
     */
    public function update(){
        $id = $this->getValidatorData('id');
        $form =  Elm::createForm($this->getRoute($id?'edit':'add'));

        $form->setRule([
            Elm::input('title','名称')->required(),
        ]);

        $id && $form->append(
            Elm::hidden('id','ID')
        );

        $lists = $form->setTitle($id?'编辑考试':'添加考试')->formData(function ()use($id){
            $formData = [];
            if($id){
                $formData = App(ExamDao::class)->where('id',$id)->first()->toArray();
            }
            return $formData;
        });
        _SUCCESS(formToData($lists));
    }


}